package com.secure.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.AntPathMatcher;

import java.util.List;
import java.util.Objects;

/**
 * 路径工具
 */
public class PathUtil {

    private final static AntPathMatcher MATCHER = new AntPathMatcher();

    /**
     * 路径是否匹配
     * <p>
     * 路径部分匹配按照AntPathMatcher匹配判断
     * 后缀部分按照全等判断
     *
     * @param patternPathList 配置路径
     * @param servletPath     请求路径
     * @param matchSuffix     匹配:后缀
     * @return
     */
    public static boolean isMatch(List<String> patternPathList, String servletPath, boolean matchSuffix) {
        if (CollectionUtil.isEmpty(patternPathList) || StrUtil.isBlank(servletPath)) {
            return false;
        }
        boolean match = false;

        for (String patternPath : patternPathList) {
            String patternPathPrefix, patternPathSuffix = null, servletPathPrefix, servletPathSuffix = null;
            if (patternPath.contains(":")) {
                patternPathPrefix = patternPath.substring(0, patternPath.indexOf(":"));
                patternPathSuffix = patternPath.substring(patternPath.indexOf(":") + 1);
            } else {
                patternPathPrefix = patternPath;
            }
            if (servletPath.contains(":")) {
                servletPathPrefix = servletPath.substring(0, servletPath.indexOf(":"));
                servletPathSuffix = servletPath.substring(servletPath.indexOf(":") + 1);
            } else {
                servletPathPrefix = servletPath;
            }
            if (MATCHER.match(patternPathPrefix, servletPathPrefix)) {
                if (matchSuffix) {
                    if (StrUtil.isBlank(patternPathSuffix) && StrUtil.isBlank(servletPathSuffix)) {
                        match = true;
                        break;
                    } else if (StrUtil.isNotBlank(patternPathSuffix) && StrUtil.isNotBlank(servletPathSuffix)) {
                        if (patternPathSuffix.equals(servletPathSuffix)) {
                            match = true;
                            break;
                        }
                    }
                } else {
                    match = true;
                    break;
                }
            }
        }
        return match;
    }

    /**
     * 路径是否匹配
     * <p>
     * 路径部分匹配按照AntPathMatcher匹配判断
     *
     * @param patternPathList 配置路径
     * @param servletPath     请求路径
     * @return
     */
    public static boolean isMatch(List<String> patternPathList, String servletPath) {
        if (CollectionUtil.isEmpty(patternPathList) || StrUtil.isBlank(servletPath)) {
            return false;
        }
        return patternPathList.stream().anyMatch(patternPath -> isMatch(patternPath, servletPath));
    }

    /**
     * 匹配
     *
     * @param patternPath 配置路径
     * @param servletPath 请求路径
     * @return
     */
    public static boolean isMatch(String patternPath, String servletPath) {
        Objects.requireNonNull(patternPath);
        Objects.requireNonNull(servletPath);
        return MATCHER.match(patternPath, servletPath);
    }

}
